/**
 * @author rxliuli
 */
const {
    compare,
    DateFormatter: {
        dateFormatter,
    },
} = rx
const app = new BaseValidateVue({
    el: '#app',
    data() {
        return {
            form: new BaseIpAddressInfo(),
            ruleList: [
                new ValidateObject({
                    ruleName: 'checkIpArr',
                    validate: this.checkIpArrValidate,
                }),
            ],
        }
    },
    methods: {
        createValidateList() {
            return this.ruleList;
        },
        async updateBaseIpAddressInfo() {
            this.$validator.validateAll().then(async (res) => {
                if (res) {
                    if (await ajaxOption({
                        res: baseIpAddressInfoApi.update({
                            ...this.form,
                            assignUseTime: dateFormatter.format(this.form.assignUseTime),
                        }),
                        danger: '更新失败',
                        success: '更新成功',
                    })) {
                        forward('/basic-data/admin_ip_seg_info')
                    }
                } else {
                    await rxPrompt.dangerMsg("输入存在错误！");
                }
            })
        },
        async checkIpArrValidate(endIp, args) {
            const {startIp = ''} = this.form;
            const fn = val => val.split('.').slice(0, 3)
            return compare(fn(startIp), fn(endIp))
        },
        async checkIpLastValidate(endIp, args) {
            const {startIp = ''} = this.form;
            const last = val => val.split('.')[3]
            return last(endIp) - last(startIp) > 0
        },
        init() {
            const form = new BaseIpAddressInfo(forward.getData())
            this.form = {
                ...form,
                assignUseTime: new Date(form.assignUseTime),
            }
        },
    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },
    mounted() {
        this.init()
    },
})
